<?php
/**
 * Created by PhpStorm.
 * User: lk
 * Date: 2017/12/4
 * Time: 16:48
 */

namespace Portal\Service;
require_once('/simplewind/Lib/Extend/aliyun-oss/autoload.php');
//use OSS\OssClient;  //可以使用命名空间

class OssService
{
    private $oss_obj;
    private $bucket;//bucket名称
    const accessKeyId = 'LTAIqFSYs1WOpZu3';//阿里云Access Key ID
    const accessKeySecret = 'N4JD92y5gHErtLBBdasMq9n0Yq8wWy';//阿里云Access Key Secret
    const endpoint = 'oss-cn-beijing.aliyuncs.com';//bucket下的域名访问下的EndPoint值
    function __construct($bucket)
    {
        $this->bucket = $bucket;
        //初始化类
//        vendor('aliyun-oss.autoload');
        $this->oss_obj = new \OSS\OssClient(self::accessKeyId,self::accessKeySecret,self::endpoint);
//        $this->oss_obj = new OssClient(self::accessKeyId,self::accessKeySecret,self::endpoint);//命名空间用法
    }
    //上传图片
    function upload_file($name = null, $field = null){
        //处理文件名字，防止文件名字重复
        $name=time().$name;
        $result = $this->oss_obj->uploadFile($this->bucket, $name, $field);
        return $result['info']['url'];
    }
    //上传目录
    function upload_Dir($prifix,$dirs){
        $result = $this->oss_obj->uploadDir($this->bucket,$prifix,$dirs);
        return $result;
    }
    //创建目录
    function create_dir($dir_name){
        $this->oss_obj->createObjectDir($this->bucket,$dir_name);
    }
}